/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;

namespace FaceCat {
    /// <summary>
    /// 切换到上个月的按钮
    /// </summary>
    public class ArrowButton : FCButton {
        /// <summary>
        /// 创建按钮
        /// </summary>
        public ArrowButton(FCCalendar calendar) {
            m_calendar = calendar;
            setBorderColor(FCColor.None);
            setBackColor(FCColor.None);
            setSize(new FCSize(16, 16));
        }

        public FCCalendar m_calendar;

        /// <summary>
        /// 获取日历视图
        /// </summary>
        public virtual FCCalendar getCalendar()
        {
            return m_calendar;
        }

        /// <summary>
        /// 设置日历视图
        /// </summary>
        public virtual void setCalendar(FCCalendar value)
        {
            m_calendar = value;
        }

        public bool m_toLast = true;

        /// <summary>
        /// 获取是否向后
        /// </summary>
        public virtual bool isToLast()
        {
            return m_toLast;
        }

        /// <summary>
        /// 设置是否向后
        /// </summary>
        public virtual void setToLast(bool value)
        {
            m_toLast = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "ArrowButton";
        }

        /// <summary>
        /// 触摸点击事件
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onClick(FCTouchInfo touchInfo) {
            base.onClick(touchInfo);
            if (m_calendar != null) {
                FCCalendarMode mode = m_calendar.getMode();
                //日
                if (mode == FCCalendarMode.Day) {
                    if (m_toLast) {
                        m_calendar.goLastMonth();
                    }
                    else {
                        m_calendar.goNextMonth();
                    }
                }
                //月
                else if (mode == FCCalendarMode.Month) {
                    MonthDiv monthDiv = m_calendar.getMonthDiv();
                    if (monthDiv != null) {
                        int year = monthDiv.getYear();
                        if (m_toLast) {
                            monthDiv.selectYear(year - 1);
                        }
                        else {
                            monthDiv.selectYear(year + 1);
                        }
                    }
                }
                //年
                else if (mode == FCCalendarMode.Year) {
                    YearDiv yearDiv = m_calendar.getYearDiv();
                    if (yearDiv != null) {
                        int year = yearDiv.getStartYear();
                        if (m_toLast) {
                            yearDiv.selectStartYear(year - 12);
                        }
                        else {
                            yearDiv.selectStartYear(year + 12);
                        }
                    }
                }
                m_calendar.update();
                m_calendar.invalidate();
            }
        }

        /// <summary>
        /// 重绘背景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            int width = getWidth(), height = getHeight();
            FCPoint p1 = new FCPoint(), p2 = new FCPoint(), p3 = new FCPoint();
            //计算三个点的位置
            if (m_toLast) {
                p1.x = 0;
                p1.y = height / 2;
                p2.x = width;
                p2.y = 0;
                p3.x = width;
                p3.y = height;
            }
            else {
                p1.x = 0;
                p1.y = 0;
                p2.x = 0;
                p2.y = height;
                p3.x = width;
                p3.y = height / 2;
            }
            FCPoint[] points = new FCPoint[3];
            points[0] = p1;
            points[1] = p2;
            points[2] = p3;
            paint.fillPolygon(getPaintingTextColor(), points);
        }
    }
}
